package tsm.magic.handler;
import tsm.activity.NotificationActivity;
import tsm.bean.request.MagicPageUploadBean;
import tsm.bean.response.LoginResultBean;
import tsm.bean.response.NotifyChangeStatusBean;
import tsm.constants.Language;
import tsm.model.user.User;
import tsm.network.RequestMessage;
import tsm.network.ResponseMessage;
import tsm.service.helper.IResourceService;
import tsm.service.helper.ITaskScheduler;
import tsm.service.magic.IMagicPageService;
import tsm.service.payment.ISMSManager;
import tsm.service.user.IPSService;
import tsm.service.user.ISessionService;
import tsm.service.user.ISubscriberService;
import tsm.service.user.IUserService;
import tsm.util.SerializerHelper;

public class LoginPageHandler extends MagicPageHandler {
	private Logger log = LoggerFactory.getLogger(LoginPageHandler.class);
	private final IUserService userService;
	private final IPSService psService;
	private final ISubscriberService subscriberService;
	private final ISessionService sessionService;
	private final IResourceService resourceService;
	private final MainPageHandler homePageHandler;
	private final SocialPageHandler socialPageHandler;
	private final ISMSManager smsManager;
	private final ITaskScheduler taskScheduler;

	@Inject
	private LoginPageHandler(IMagicPageService magicPageService) {
		super(magicPageService);
	}

	@Override
	public void processRequest(IoSession session, RequestMessage message) {
		byte lang = 0;
		try {
			lang = (Byte) session.getAttribute(UserConstant.LANGUAGE);
		} catch (Exception e) {
		}
		MagicPageUploadBean magicPageUploadBean = (MagicPageUploadBean) message.getBean();
		IoBuffer buffer = IoBuffer.wrap(magicPageUploadBean.getUploadData());
		int uploadType = SerializerHelper.readInt(buffer);
		switch (uploadType) {
		case UPLOAD_TYPE_LOGIN_PAGE:
			processActionLogin(session, buffer, lang);
			break;
		case UPLOAD_TYPE_CLOSE_ACTIVATE_PAGE: {
			processCloseActivatePage(session, buffer, lang);
			break;
		}
		default:
			//			log.info("LoginPageHandler processRequest called not process");
			break;
		}
	}

	@Override
	public int getBeanTypeListen() {
		return MAGIC_PAGE_UPLOAD_BEAN;
	}

	public byte[] createLoginPage(byte language) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			MagicGenerator.closePage(buffer, 0, true); // close currentPage if
														// exist
			MagicGenerator.createPage(buffer, MagicConstant.SCROLLABLE_Y, MagicConstant.BOXLAYOUT_Y, (byte) -1, (byte) -1, Language.LOGIN_TITLE[language], -1, (byte) 0,
					LOGIN_PAGE_ID);

			MagicGenerator.addLabelToPage(buffer, -1, MagicConstant.NONE_CONSTRAINT, MagicConstant.TOP, MagicConstant.CENTER, resourceService.getImageByName("res/logo.png"),
					(byte) -1, "", -1, (byte) 1, false, -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, 1);

			MagicGenerator.addLabelToPage(buffer, -1, MagicConstant.NONE_CONSTRAINT, MagicConstant.LEFT, MagicConstant.LEFT, null, (byte) -1, Language.USERNAME[language],
					MagicColor.XANH_BIEN, (byte) 0, false, -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, 2);
			MagicGenerator.addTextFieldToPage(buffer, -1, MagicConstant.NONE_CONSTRAINT, "$:defaultUsername", -1, MagicConstant.ANY, MagicConstant.INPUT_MODE_abc, 256, true, true,
					3);

			MagicGenerator.addLabelToPage(buffer, -1, MagicConstant.NONE_CONSTRAINT, MagicConstant.LEFT, MagicConstant.LEFT, null, (byte) -1, Language.PASSWORD[language],
					MagicColor.XANH_BIEN, (byte) 0, false, -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, 4);
			MagicGenerator.addTextFieldToPage(buffer, -1, MagicConstant.NONE_CONSTRAINT, "$:defaultPassword", -1, MagicConstant.PASSWORD, MagicConstant.INPUT_MODE_abc, 256, true,
					true, 5);

			MagicGenerator.addContainerToPage(buffer, 1000, MagicConstant.NONE_CONSTRAINT, MagicConstant.NONE_SCROLL, MagicConstant.FLOWLAYOUT_CENTER, (byte) -1, (byte) -1,
					(byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1);
			MagicGenerator.addButtonToPage(buffer, 1000, MagicConstant.NONE_CONSTRAINT, MagicConstant.TOP, MagicConstant.CENTER, null, (byte) -1, null, (byte) -1, (byte) 1,
					Language.LOGIN[language], -1, (byte) -1, (byte) -1, (byte) -1, true, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1,
					createActionLogin(), 6);

			MagicGenerator.addCommandToPage(buffer, Language.SUPPORT[language], (byte) 0, null, MagicGenerator.createActionOpenUrl("tel:01284926919"));
			MagicGenerator.addCommandToPage(buffer, Language.EXIT[language], (byte) 1, null, MagicGenerator.createActionExitApp());
//			MagicGenerator.addCommandToPage(buffer, Language.HOME_PAGE[language], (byte) 1, null,
//					MagicGenerator.createActionShowDialogInfo(Language.INFO[language], Language.WEBSITE_CONTENT[language]));
			MagicGenerator.addCommandToPage(buffer, Language.REGISTER[language], (byte) 0, null, createActionGotoRegisterPage());

			MagicGenerator.requestFocus(buffer, 6);

			MagicGenerator.endPage(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("createLoginPage ", e);
		}
		return null;
	}

	private byte[] createActionLogin() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			SerializerHelper.writeInt(MagicConstant.UPLOAD_DATA_COMMAND, buffer);
			SerializerHelper.writeInt(UPLOAD_TYPE_LOGIN_PAGE, buffer);
			SerializerHelper.writeInt(LOGIN_PAGE_ID, buffer); // pageId
			SerializerHelper.writeInt(2, buffer); // n param

			SerializerHelper.writeByte(MagicConstant.STRING_PARAMENTERS, buffer);
			SerializerHelper.writeByte(MagicConstant.VALUE_GET_FROM_COMPONENTS, buffer);
			SerializerHelper.writeInt(3, buffer); // compId
			SerializerHelper.writeBoolean(true, buffer); // must fill

			SerializerHelper.writeByte(MagicConstant.STRING_PARAMENTERS, buffer);
			SerializerHelper.writeByte(MagicConstant.VALUE_GET_FROM_COMPONENTS, buffer);
			SerializerHelper.writeInt(5, buffer); // compId
			SerializerHelper.writeBoolean(true, buffer); // must fill

			// show dialog
			MagicGenerator.endCommand(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("createActionLogin ", e);
		}
		return null;
	}

	private void processActionLogin(IoSession session, IoBuffer buffer, byte lang) {
		
	}

	private byte[] createActionGotoRegisterPage() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			SerializerHelper.writeInt(MagicConstant.UPLOAD_DATA_COMMAND, buffer);
			SerializerHelper.writeInt(UPLOAD_TYPE_GOTO_REGISTER_PAGE, buffer);
			SerializerHelper.writeInt(0, buffer); // n param

			// show dialog
			MagicGenerator.endCommand(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("createActionGotoRegisterPage ", e);
		}
		return null;
	}

	public byte[] createActivatePage(byte language, int partnerId, String username, String password) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			MagicGenerator.createPage(buffer, MagicConstant.SCROLLABLE_Y, MagicConstant.BOXLAYOUT_Y, (byte) -1, (byte) -1, Language.ACTIVATE_PAGE[language], -1, (byte) 0,
					ACTIVATE_PAGE_ID);

			MagicGenerator.addContainerToPage(buffer, 500, MagicConstant.NONE_CONSTRAINT, MagicConstant.NONE_SCROLL, MagicConstant.BORDERLAYOUT, (byte) -1, (byte) -1, (byte) -1,
					(byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1);
			MagicGenerator.addTextAreaToPage(buffer, 500, MagicConstant.CENTER_CONSTRAINT, Language.ACTIVATE_GUIDE[language], MagicColor.WHITE, (byte) -1, MagicConstant.ANY, 256,
					false, false, MagicColor.BLACK, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, 501);

			SMSInfo smsInfo = smsManager.getSMSInfoByPartner(partnerId);
			StringBuilder sb = new StringBuilder();
			sb.append(smsInfo.getSyntaxActivate());
			sb.append(username);
			sb.append(" ");
			sb.append(password);
			MagicGenerator.addContainerToPage(buffer, 1000, MagicConstant.NONE_CONSTRAINT, MagicConstant.NONE_SCROLL, MagicConstant.FLOWLAYOUT_CENTER, (byte) -1, (byte) -1,
					(byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1);
			MagicGenerator.addButtonToPage(buffer, 1000, MagicConstant.NONE_CONSTRAINT, MagicConstant.TOP, MagicConstant.CENTER, null, (byte) -1, null, (byte) -1, (byte) 1,
					Language.ACTIVATE_NOW[language], -1, (byte) -1, (byte) -1, (byte) -1, true, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1, (byte) -1,
					(byte) -1, MagicGenerator.createActionSendSMS(sb.toString(), smsInfo.getPhoneActivate()), 6);

			MagicGenerator.addCommandToPage(buffer, Language.CLOSE[language], (byte) 0, null, createActionCloseActivatePage());

			MagicGenerator.requestFocus(buffer, 6);

			MagicGenerator.endPage(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("createActivatePage ", e);
		}
		return null;
	}

	private byte[] createActionCloseActivatePage() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			SerializerHelper.writeInt(MagicConstant.UPLOAD_DATA_COMMAND, buffer);
			SerializerHelper.writeInt(UPLOAD_TYPE_CLOSE_ACTIVATE_PAGE, buffer);
			SerializerHelper.writeInt(0, buffer); // n param

			// show dialog
			MagicGenerator.endCommand(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("createActionCloseActivatePage ", e);
		}
		return null;
	}

	private void processCloseActivatePage(IoSession session, IoBuffer buffer, byte lang) {
		MagicUtil.sendPageToClient(MagicGenerator.createActionClosePage(ACTIVATE_PAGE_ID, true), session);
		String userName = (String) session.getAttribute(UserConstant.USERNAME);
		MagicUtil.sendMagicCheckSum(MagicPageIdConstant.MAIN_PAGE_ID, homePageHandler.createMainPage(lang, userName), session);
	}

}
